package com.modifiertech.lineage.skills;

import com.feed_the_beast.ftblib.lib.icon.Icon;
import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.api.PlayerDataSkill;
import com.modifiertech.lineage.base.ConfigHandler;
import com.modifiertech.lineage.client.gui.ftbint.GuiIconsV2;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.event.SkillAnimateCallEvent;
import com.modifiertech.lineage.handler.PlayerManaModifyHandler;

import com.modifiertech.lineage.network.MessageClientSay;
import com.modifiertech.lineage.network.NetworkHandler;
import com.modifiertech.lineage.utils.AABBUtil;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.gameevent.TickEvent;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**范围回血
 * 
 */
public class RangeHealth extends PlayerDataSkill {
    public static final String skillName = "群体治疗";
    private static final Random random = new Random();
    @Override
    protected Icon defineSkillIcon() {
        return GuiIconsV2.RangeHealth;
    }

    @Override
    protected String defineSkillType() {
        return BaseConst.PLAYER_ADV_TAG__SKILLS_SPE;
    }

    @Override
    protected String defineSkillSecondType() {
        return BaseConst.PLAYER_ADV_SEC_TAG__SKILLS_ONCTWO;
    }

    @Override
    protected String defineSkillName() {
        return skillName;
    }

    @Override
    protected int defineSkillLevelDependency() {
        return 3;
    }

    @Override
    public String defineSkillDesc() {
        int skillLevel = getSkillLevel();
        return "可群体治疗目标范围内所有单位\n" +
                "§4§l[变种:自疗]技能持有阶段再次触发则以当前玩家为中心进行触发" +
                (skillLevel>0?"\n效果:单位治疗 §6§l"+getLevelHeal()+" 点血量":"") ;
    }

    @Override
    protected double defineSkillRowX() {
        return 4;
    }

    @Override
    protected double defineSkillColumnY() {
        return 1;
    }

    @Override
    protected int defineSkillCoolDown() {
        return ConfigHandler.skillRangeHealthCoolDown;
    }

    @Override
    protected double defineSkillManaUse() {
        return 5;
    }

    @Override
    protected List<String> defineSkillDependencyName() {
        return new ArrayList<>(Arrays.asList(new String[]{}));
    }

    @Override
    protected int defineSkillRegion() {
        return 0;
    }

    @Override
    protected int defineUpgradePoint() {
        return 1;
    }

    @Override
    public void onSkillAnimateCall(SkillAnimateCallEvent.Post event) {
        PlayerData playerData = PlayerDataHandler.get(event.getEntityPlayer());
        //持有状态在触发一次表示以自身为原点进行技能结算 技能变种:自疗
        if (playerData.checkSkillHanding(getSkillName())){
            playerData.updateSkillHandingAndSync(getSkillName(),false, (EntityPlayerMP) event.getEntityPlayer());
            playerData.updateSkillRangeAvailableStatusAndSync(true,(EntityPlayerMP) event.getEntityPlayer());
            //根据玩家位置计算AABB盒
            BlockPos centerPos =event.getEntityPlayer().getPosition();
            AxisAlignedBB axisAlignedBB = AABBUtil.getAABBFromCenterPos(centerPos,3,3);
            //覆盖AABB
            List<AxisAlignedBB> axisAlignedBBS = new ArrayList<>();
            axisAlignedBBS.add(axisAlignedBB);
            playerData.updateSkillRangeAABBListAndSync(axisAlignedBBS,(EntityPlayerMP) event.getEntityPlayer());//todo check
            //触发结算
            onPlayerSkillHandingOff((EntityPlayerMP) event.getEntityPlayer());
        }else{
            //检查当前技能是否可用(是否处于CD 等)
            if (!playerData.checkOnceTypeSkillAvailable(getSkillName())) return;
            if (!playerData.checkSkillCdAvailable(getSkillName())){
                return;
            }
            if (!playerData.checkSkillManaAvailable(defineSkillManaUse())){
                NetworkHandler.INSTANCE.sendTo(new MessageClientSay().write("魔力不足!"), (EntityPlayerMP) event.getEntityPlayer());
                return;
            }

            //激活技能持有状态
            playerData.updateSkillHandingAndSync(getSkillName(), true, (EntityPlayerMP) event.getEntityPlayer());
            playerData.turnOnSkillExecutedDelay(getSkillName(), 5);
            //扣减魔法
            PlayerManaModifyHandler.INSTANCE.manaModify(event.getEntityPlayer(),0-defineSkillManaUse(),playerData);
            //设定技能cd
            playerData.setSkillCdAndSync(getSkillName(),defineSkillCoolDown(), (EntityPlayerMP) event.getEntityPlayer());
        }
    }

    @Override
    public void onSkillRangeSelectedRender(float partialTicks) {
        Lineage.proxy.handleRenderWorldLastEventRangeHealth(partialTicks);
    }

    @Override
    public void onPlayerTick(TickEvent.PlayerTickEvent event) {
        Lineage.proxy.handleOnPlayerTickRangeHealth(event);
    }

    @Override
    public void onPlayerSkillHandingOff(EntityPlayer entityPlayer) {
        Lineage.proxy.handleOnPlayerSkillHandingOffRangeHealth(entityPlayer,getLevelHeal(),random);
    }

    /**添加手部粒子效果
     * 
     */
    public static void handingRender(TickEvent.PlayerTickEvent event,EntityPlayer player) {
        // 仅在服务器端执行
            WorldServer worldServer = (WorldServer) player.world;

            // 获取玩家手部位置
            Vec3d handPos = player.getPositionEyes(event.phase == TickEvent.Phase.START ? 1.0F : 0.0F);
//            EnumHand hand = EnumHand.OFF_HAND; // 或 EnumHand.MAIN_HAND

            double handX = handPos.x + player.getLookVec().x * 0.5;
            double handY = handPos.y - 0.4 + player.getLookVec().y * 0.5;
            double handZ = handPos.z + player.getLookVec().z * 0.5;

            // 设置绿色的RGB值，并通过speedX调整粒子大小
            float red = 0.001F;  // 不能为 0，否则粒子会不可见
            float green = 1.0F;
            float blue = 0.0F;
            float particleSize = 2F; // 通过调整这个值来增加粒子大小

            // 生成粒子效果
            worldServer.spawnParticle(EnumParticleTypes.REDSTONE, handX, handY, handZ, 0, red * particleSize, green * particleSize, blue * particleSize, 1.0);
    }

    private float getLevelHeal(){
        return new BigDecimal(5 + (2 * getSkillLevel()))
                .setScale(2, RoundingMode.HALF_UP)
                .floatValue();
    }
    
    
    
    
    

}
